home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_010 / iff / unpacker.c < prev   
C/C++ Source or Header  |  1992-05-06  |  2KB  |  63 lines

  1. /*----------------------------------------------------------------------*
  2.  * unpacker.c Convert data from "cmpByteRun1" run compression. 11/15/85
  3.  *
  4.  * By Jerry Morrison and Steve Shaw, Electronic Arts.
  5.  * This software is in the public domain.
  6.  *
  7.  *      control bytes:
  8.  *       [0..127]   : followed by n+1 bytes of data.
  9.  *       [-1..-127] : followed by byte to be repeated (-n)+1 times.
  10.  *       -128       : NOOP.
  11.  *
  12.  * This version for the Commodore-Amiga computer.
  13.  *----------------------------------------------------------------------*/
  14.  
  15. #include "packer.h"
  16.  
  17.  
  18. /*----------- UnPackRow ------------------------------------------------*/
  19.  
  20. #define UGetByte()      (*source++)
  21. #define UPutByte(c)     (*dest++ = (c))
  22.  
  23. /* Given POINTERS to POINTER variables, unpacks one row, updating the source
  24.  * and destination pointers until it produces dstBytes bytes. */
  25.  
  26. BOOL UnPackRow(pSource, pDest, srcBytes0, dstBytes0)
  27. BYTE **pSource, **pDest;
  28. WORD srcBytes0, dstBytes0;
  29. {
  30.     register BYTE *source = *pSource;
  31.     register BYTE *dest   = *pDest;
  32.     register WORD n;
  33.     register BYTE c;
  34.     register WORD srcBytes = srcBytes0, dstBytes = dstBytes0;
  35.     BOOL error = TRUE;  /* assume error until we make it through the loop */
  36.     WORD minus128 = -128;  /* get the compiler to generate a CMP.W */
  37.  
  38.     while( dstBytes > 0 )  {
  39.         if ( (srcBytes -= 1) < 0 )  goto ErrorExit;
  40.         n = UGetByte();
  41.  
  42.         if (n >= 0) {
  43.             n += 1;
  44.             if ( (srcBytes -= n) < 0 )  goto ErrorExit;
  45.             if ( (dstBytes -= n) < 0 )  goto ErrorExit;
  46.             do {  UPutByte(UGetByte());  } while (--n > 0);
  47.             }
  48.  
  49.         else if (n != minus128) {
  50.             n = -n + 1;
  51.             if ( (srcBytes -= 1) < 0 )  goto ErrorExit;
  52.             if ( (dstBytes -= n) < 0 )  goto ErrorExit;
  53.             c = UGetByte();
  54.             do {  UPutByte(c);  } while (--n > 0);
  55.             }
  56.         }
  57.     error = FALSE;      /* success! */
  58.  
  59.   ErrorExit:
  60.     *pSource = source;  *pDest = dest;
  61.     return(error);
  62. }
  63.